package com.app.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

@Component
public class AliSMS {

    private static String signName = "安定区人力资源服务平台";

    private static String templateCode = "SMS_257655980";

    private static String accessKeyId = "LTAI5tCTaAkBVkdeRoVvQxUh";

    private static String secret = "C1QgyVvPTguTohWxJzkIApZ8rmqnaE";
    /**
     * 发送短信
     * @param phoneNumbers 手机号
     * @param code 验证码
     */
    public static void sendMessage(String phoneNumbers, String code){
        //default 地域节点，默认就好；  后面是 阿里云的 id和秘钥
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId,secret);
        IAcsClient client = new DefaultAcsClient(profile);
        //这里不能修改
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");    //短信服务的服务接入地址
        request.setSysVersion("2017-05-25");        //API的版本号
        request.setSysAction("SendSms");    //API的名称
        request.putQueryParameter("PhoneNumbers", phoneNumbers);    //接收短信的手机号码
        request.putQueryParameter("SignName", signName);//    短信签名名称
        request.putQueryParameter("TemplateCode", templateCode);//    短信模板Code，注意这个模板要和签名对应
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}"); //短信模板变量对应的实际值
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 个人求职
     * @param phoneNumbers
     */
    public static void regTimeMessage(String phoneNumbers,String templateCode){
        //default 地域节点，默认就好；  后面是 阿里云的 id和秘钥
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId,secret);
        IAcsClient client = new DefaultAcsClient(profile);
        //这里不能修改
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");    //短信服务的服务接入地址
        request.setSysVersion("2017-05-25");        //API的版本号
        request.setSysAction("SendSms");    //API的名称
        request.putQueryParameter("PhoneNumbers", phoneNumbers);    //接收短信的手机号码
        request.putQueryParameter("SignName", signName);//    短信签名名称
        request.putQueryParameter("TemplateCode", templateCode);//    短信模板Code，注意这个模板要和签名对应
//        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}"); //短信模板变量对应的实际值
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}


